#include "ECG_EEGDataCollectAna/login_ecgeeg.h"
#include <HotPlot.h>
#include <QApplication>
#include "QFile"
#include "qsettings.h"
#include "menuform.h"
#include "charts/waveformchart.h"
#include "HRVAna/lombscarglecalculator.h"
#include "ECG_EEGDataAna/ecgprocess.h"
#include "opencv2/opencv.hpp"
#include "HRVAna/hrvana.h"
#include "HRVAna/hrvas.h"
#include "shareAlg/wavelet/wavelet.h"
#include <HRVAna/arpsdcalculator.h>
#include <HRVAna/cubicspline.h>
#include <HRVAna/splineinterpolator.h>

void testwavelet();
void initSettings();
void setStyle();
void testlomb();
void testAr();
void testinter();


int main(int argc, char * argv[]) {
    QApplication a(argc, argv);
    setStyle();
    initSettings();
    // Login_ECGEEG w;
    MenuForm w;
    // HotPlot w;
    w.show();
    return a.exec();
}
void setStyle()
{
    QFile file(":/qss/black.css");
    QString styleSheet;
    file.open(QFile::ReadOnly);
    styleSheet = QLatin1String(file.readAll());
    qApp->setStyleSheet(styleSheet);
    file.close();
}

void initSettings()
{

    // QSettings settings(QSettings::NativeFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
    QCoreApplication::setOrganizationName("xinyu");
    QCoreApplication::setApplicationName("xinlab");


}

void testwavelet()
{
    // 测试信号
    QVector<double> x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    int n = 6; // 分解级别
    QString wname = "db1"; // 小波名称

    // 小波分解
    Wavelet wavelet(wname);
    QVector<double> c;
    QVector<int> l;
    wavelet.wavedec(x,  n, c, l);

    // 输出分解结果
    qDebug() << "Decomposition coefficients (c):" << c;
    qDebug() << "Lengths by level (l):" << l;

    // 小波重构
    QVector<double> reconstructedX;
    wavelet.waverec(c,  l, reconstructedX);

    // 输出重构结果
    qDebug() << "Reconstructed signal:" << reconstructedX;
}
void testAr()
{
    // 创建 ARPSDCalculator 对象
    ARPSDCalculator calculator;
    Eigen::VectorXd t(10);
    Eigen::VectorXd y(10);
    t << 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
    // y << 1, 2, 1.5, 3, 2.5, 4, 3.5, 5, 4.5, 6;
    // 计算 AR 模型 PSD

    Eigen::VectorXd dd=calculator.interpolate(t, t, 4);
    qDebug()<<calculator.eigenToQVector(dd);
    // QVector<double>t2;
    // QVector<double>t1;
    // t1.append(0);t1.append(1);
    // t2.append(0.25);
    // t2.append(0.5);
    // QVector<double> dd2=calculator.interp1(t1, t1, t2, "spline") ;
    // qDebug()<<dd2;

    // auto result = calculator.calculate(t,  y, 4,100,16);
    // QVector<double> PSD = result.first;
    // QVector<double> F = result.second;

    // 输出结果
    // for (int i = 0; i < PSD.size();  ++i) {
    //     qDebug() << "F[" << i << "] = " << F[i] << ", PSD[" << i << "] = " << PSD[i]<<Qt::endl;
    // }
}
void testlomb()
{
    // 示例数据
    Eigen::VectorXd t(10);
    Eigen::VectorXd y(10);
    t << 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
    y << 1, 2, 1.5, 3, 2.5, 4, 3.5, 5, 4.5, 6;

    // 创建 LombScargleCalculator 对象
    LombScargleCalculator calculator;

    // 调用 calculate 函数
    auto result = calculator.Lombcalculate(t,  y, 100, 1.0);
    QVector<double> PSD = result.first;
    QVector<double> F = result.second;
    qDebug()<<PSD;
    qDebug()<<F;
}

void testinter()
{
    // 示例数据
    // QVector<double> t = {0, 1, 2, 3, 4};
    // QVector<double> y = {1, 3, 2, 4, 5};
    // QVector<double> t2 = {0.5, 1.5, 2.5, 3.5};

    // // 创建样条插值对象
    // SplineInterpolator interpolator;

    // // 进行插值
    // QVector<double> y2 = interpolator.interpolate(t,  y, t2);

    // // 输出结果
    // for (double val : y2) {
    //     qDebug() << val << " ";
    // }

    QVector<double> t = {0, 1, 2, 3, 4};
    QVector<double> y = {1, 3, 2, 4, 5};
    // std::vector<double> t2 = {0.5, 1.5, 2.5, 3.5};
    CubicSpline spline(t, y);
    // 在x=1.5处插值
    double value = spline.interpolate(0.5); // 应接近3.375
    qDebug()<<"value"<<value;
}
